$(function() {
    load();
});

var pages = 1;
function load() {
    layui.use(['table', 'dropdown','form'], function(){
        var table = layui.table,
            form = layui.form ,
            $ = layui.$;
        let colItem= [
            {
                field: 'commentTypeName',
                title: '评论类型',
                align: 'center',
            },
            // {
            //     field: 'commentType',
            //     title: '评论类型',
            //     align: 'center',
            // },
            {
                field : 'createTime',
                title : '创建时间',
                align : 'center'
            },
            {
                field : 'status',
                title : '状态',
                align : 'center',
                templet:(d)=>{
                    return upAndDownPrices?((d.status=='0'?`   <input type="checkbox" checked `:'<input type="checkbox" ')+` name="open"  value="${d.id}"
      title="启用|禁用" myid="${d.id}" lay-skin="switch" lay-filter="switchTest" title="是否启用"> 
						`):(d.status=='0'?'<span class="label label-primary">启用</span>':'<span class="label label-warning">禁用</span>')
                }
            }
        ]
        if(s_edit_h==''||s_remove_h==''){
            colItem.push(
                {
                    title : '操作',
                    field : 'id',
                    align : 'center',
                    width:160,
                    templet : function(d) {
                        var b2 = '<a  class="btn btn-primary btn-sm '+s_edit_h+' " href="#" mce_href="#" title="编辑" onclick="edit(\''
                            + d.id
                            + '\')"><i class="fa fa-edit"></i></a> ';

                        var ss = '<a class="btn btn-success btn-sm '+s_remove_h+' " href="#" title="删除"  mce_href="#" onclick="remove(\''
                            + d.id
                            + '\')"><i class="fa fa-remove"></i></a>';
                        return  b2 +   ss;
                    }
                }
            )
        }
        const cols=[colItem]
        // 创建渲染实例
        table.render({
            elem: '#exampleTable'
            ,id:'exampleTable'
            ,method:"get"
            // ,contentType: 'application/json'
            ,url: "/jazUserCommentClassify/list"
            ,cellMinWidth: 80
            , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                , curr: pages //设定初始在第 5 页
                , groups: 5 //只显示 1 个连续页码
                , limit : 10
            }
            ,request:{
                pageName: 'pageNumber',
                limitName: 'pageSize'
            }
            ,where: {
            }
            ,defaultToolbar: false
            ,loading: true
            ,cols: [[
                {
                    field: 'commentTypeName',
                    title: '评论类型',
                    align: 'center',
                },
                // {
                //     field: 'commentType',
                //     title: '评论类型',
                //     align: 'center',
                // },
                {
                    field : 'createTime',
                    title : '创建时间',
                    align : 'center'
                },
                {
                    field : 'status',
                    title : '状态',
                    align : 'center',
                    templet:(d)=>{
                        return upAndDownPrices?((d.status=='0'?`   <input type="checkbox" checked `:'<input type="checkbox" ')+` name="open"  value="${d.id}"
      title="启用|禁用" myid="${d.id}" lay-skin="switch" lay-filter="switchTest" title="是否启用"> 
						`):(d.status=='0'?'<span class="label label-primary">启用</span>':'<span class="label label-warning">禁用</span>')
                    }
                },
                {
                    title : '操作',
                    field : 'id',
                    align : 'center',
                    width:160,
                    templet : function(d) {
                        var b2 = '<a  class="btn btn-primary btn-sm '+s_edit_h+' " href="#" mce_href="#" title="编辑" onclick="edit(\''
                            + d.id
                            + '\')"><i class="fa fa-edit"></i></a> ';

                        var ss = '<a class="btn btn-success btn-sm '+s_remove_h+' " href="#" title="删除"  mce_href="#" onclick="remove(\''
                            + d.id
                            + '\')"><i class="fa fa-remove"></i></a>';
                        return  b2 +   ss;
                    }
                }
            ]]
            ,response: {
                statusCode: 0 //重新规定成功的状态码为 200，table 组件默认为 0
            }
            ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                if( res.code != 0 ){

                    return false;
                }

                // let screen = [];
                // res.data.records.forEach(function (v,k){
                // 	if( v.auditStatus != 0 ){
                // 		screen.push(v);
                // 	}
                // })

                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            }
            ,jump: function (obj, first) {
                //首次不执行
                if (!first) {
                    pages = obj.curr
                }
            }
            ,error: function(res, msg){

            }

            ,done: function (res){
                form.render()
            }
        });

        var $ = layui.$, active = {
            reload: function(){
                //执行重载
                table.reload('exampleTable', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                    }
                });
            }
        };
        form.on('switch(switchTest)', function(data){
            let id=$(data.othis[0].parentNode).find('input').attr('myid')
            var loadIndex = layer.load(0);
            AjaxPost('/jazUserCommentClassify/updateUserCommentClassifyStatus', {id,status:this.checked?'0':'1'}).then(function (response){
                parent.layer.msg("操作成功");
                layer.close(loadIndex)
                load();
            }).catch(error => {
                layer.msg(error.msg)
                layer.close(loadIndex)
                load();
            })




        })
        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //触发表格复选框选择
        table.on('checkbox(test)', function(obj){
            console.log(obj)
        });

        //触发表格单选框选择
        table.on('radio(test)', function(obj){
            console.log(obj)
        });

    });

}

function addEvaluationDescription(){
    let toIndex= layer.open({
        type: 2,
        title:'新增类型',
        maxmin: true,
        shadeClose: false,
        area:['800px','500px'],
        content:  `/jzweb/jazUserComment/evaluationClassification/info/1/add`
    });
    layer.full(toIndex);
}
function edit(id){
    let toIndex= layer.open({
        type: 2,
        title:'修改类型',
        maxmin: true,
        shadeClose: false,
        area:['800px','500px'],
        content:  `/jzweb/jazUserComment/evaluationClassification/info/${id}/edit`
    });
    layer.full(toIndex);
}

function remove(id) {
    layer.confirm('确定要删除选中的记录？', {
        btn : [ '确定', '取消' ]
    }, function() {
        $.ajax({
            url : '/jazUserCommentClassify/remove',
            type : "post",
            data : {
                'id' : id
            },
            success : function(r) {
                if (r.code==0) {
                    layer.msg(r.msg);
                    load();
                }else{
                    layer.msg(r.msg);
                }
            }
        });
    })
}